<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Execute a database query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb-driver-manager.executecommand.html">« MongoDB\Driver\Manager::executeCommand</a></li>
      <li style="float: right;"><a href="mongodb-driver-manager.executereadcommand.html">MongoDB\Driver\Manager::executeReadCommand »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-driver-manager.html">MongoDB\Driver\Manager</a></li>
    <li>Execute a database query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-driver-manager.executequery" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\Manager::executeQuery</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\Manager::executeQuery</span> &mdash; <span class="dc-title">Execute a database query</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-manager.executequery-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\Manager::executeQuery</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$namespace</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-query.html" class="type MongoDB\Driver\Query">MongoDB\Driver\Query</a></span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span>|<span class="type">null</span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>

  <p class="para rdfs-comment">
   Selects a server according to the <code class="literal">&quot;readPreference&quot;</code> option
   and executes the query on that server. By default, the read preference from
   the <a href="mongodb-driver-manager.construct.html#mongodb-driver-manager.construct-uri" class="link">MongoDB Connection
   URI</a> will be used.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-manager.executequery-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
   
    <dt>
<code class="parameter">namespace</code> (<span class="type">string</span>)</dt>

    <dd>

     <p class="para">
      A fully qualified namespace (e.g. <code class="literal">&quot;databaseName.collectionName&quot;</code>).
     </p>
    </dd>

   

   
   
    <dt>
<code class="parameter">query</code> (<span class="classname"><a href="class.mongodb-driver-query.html" class="classname">MongoDB\Driver\Query</a></span>)</dt>

    <dd>

     <p class="para">
      The query to execute.
     </p>
    </dd>

   

   
    <dt>
<code class="parameter">options</code></dt>

    <dd>

     <p class="para">
      <table class="doctable table">
       <caption><strong>options</strong></caption>
       
        <thead>
         <tr>
          <th>Option</th>
          <th>Type</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         
         <tr>
          <td>readPreference</td>
          <td><span class="classname"><a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a></span></td>
          <td>
           <p class="para">
            A read preference to use for selecting a server for the operation.
           </p>
          </td>
         </tr>


         
         <tr>
          <td>session</td>
          <td><span class="classname"><strong class="classname">MongoDB\Driver\Session</strong></span></td>
          <td>
           <p class="para">
            A session to associate with the operation.
           </p>
          </td>
         </tr>


        </tbody>
       
      </table>

     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb-driver-manager.executequery-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">Returns <span class="classname"><a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a></span> on success.</p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-manager.executequery-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li><li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-connectionexception.html" class="classname">MongoDB\Driver\Exception\ConnectionException</a></span> if connection to the server fails (for reasons other than authentication).</li><li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-authenticationexception.html" class="classname">MongoDB\Driver\Exception\AuthenticationException</a></span> if authentication is needed and fails.</li>
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-runtimeexception.html" class="classname">MongoDB\Driver\Exception\RuntimeException</a></span> on other errors (e.g. invalid query operators).</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-manager.executequery-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.4.0</td>
       <td>
        The third parameter is now an <code class="parameter">options</code> array.
        For backwards compatibility, this paramater will still accept a
        <span class="classname"><a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a></span> object.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-manager.executequery-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1300">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\Driver\Manager::executeQuery()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost:27017"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$bulk&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">BulkWrite</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">([</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">([</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">([</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeBulkWrite</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.collection'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$filter&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">]];<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'projection'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'sort'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">],<br />];<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Query</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.collection'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$document</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#6 (1) {
  [&quot;x&quot;]=&gt;
  int(3)
}
object(stdClass)#7 (1) {
  [&quot;x&quot;]=&gt;
  int(2)
}
</pre></div>
   </div>
  </div>

  <div class="example" id="example-1301">
   <p><strong>示例 #2 Limiting execution time for a query</strong></p>
   <div class="example-contents"><p>
    The <code class="literal">&quot;maxTimeMS&quot;</code>
    <span class="classname"><a href="class.mongodb-driver-query.html" class="classname">MongoDB\Driver\Query</a></span> option may be used to limit the
    execution time of a query. Note that this time limit is enforced on the
    server side and does not take network latency into account. See
    <a href="https://www.mongodb.com/docs/manual/tutorial/terminate-running-operations/#maxtimems" class="link external">&raquo;&nbsp;Terminate Running Operations</a>
    in the MongoDB manual for more information.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$filter&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">]];<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'maxTimeMS'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #007700">,<br />];<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Query</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.collection'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$document</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    If the query fails to complete after one second of execution time on the
    server, a
    <span class="classname"><a href="class.mongodb-driver-exception-executiontimeoutexception.html" class="classname">MongoDB\Driver\Exception\ExecutionTimeoutException</a></span>
    will be thrown.
   </p></div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodb-driver-manager.executequery-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="classname"><a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a></span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-driver-query.html" class="classname">MongoDB\Driver\Query</a></span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a></span></li>
   <li class="member"><span class="function"><a href="mongodb-driver-server.executequery.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Server::executeQuery()</a> - Execute a database query on this server</span></li>
  </ul>
 </div>


</div></div></div></body></html>